package be.jabapage.snooker.service.importation.impl;

import java.io.Serializable;
import java.util.logging.Logger;

import org.apache.commons.lang.Validate;

import be.jabapage.snooker.container.ClubContainer;
import be.jabapage.snooker.container.CompetitionContainer;
import be.jabapage.snooker.container.RegionClubContainer;
import be.jabapage.snooker.exception.EntityNotFoundException;
import be.jabapage.snooker.exception.MultipleInstanceFoundException;
import be.jabapage.snooker.exception.ProcessingException;
import be.jabapage.snooker.jdo.administration.Region;
import be.jabapage.snooker.service.administration.api.IRegionService;
import be.jabapage.snooker.service.importation.api.IClubImportService;
import be.jabapage.snooker.service.importation.api.ICompetitionImportService;
import be.jabapage.snooker.service.importation.api.IRegionImportService;
import be.jabapage.snooker.service.overwriter.api.IRegionOverwriter;

/**
 * @author axesq
 *
 */
public class RegionImportService implements IRegionImportService, Serializable {

	private static final long serialVersionUID = 3549380308389720879L;

	private static final Logger LOGGER = Logger.getLogger(RegionImportService.class.getName());

	private final IRegionService regionService;
	private final IClubImportService clubImportService;
	private final IRegionOverwriter regionOverwriter;
	private final ICompetitionImportService competitionImportService;

	/**
	 * @param regionService
	 * @param clubImportService
	 * @param regionOverwriter
	 */
	public RegionImportService(final IRegionService regionService, final IClubImportService clubImportService, final IRegionOverwriter regionOverwriter, final ICompetitionImportService competitionImportService) {
		Validate.notNull(regionService);
		Validate.notNull(clubImportService);
		Validate.notNull(regionOverwriter);
		Validate.notNull(competitionImportService);
		this.regionService = regionService;
		this.clubImportService = clubImportService;
		this.regionOverwriter = regionOverwriter;
		this.competitionImportService = competitionImportService;
	}


	/**
	 * {@inheritDoc}
	 */
	public void importClubs(final RegionClubContainer container) throws ProcessingException {
		LOGGER.info("START import of region");
		Validate.notNull(container,"Container is mandatory for importing the data.");
		Region region = container.getRegion();
		Validate.notNull(region, "The region is mandatory for importing a set.");
		try {
			Region regionDatabase = regionService.retrieve(region.getName());
			regionOverwriter.overwrite(regionDatabase, region);
			regionService.store(region);
		} catch (MultipleInstanceFoundException e) {
			throw new ProcessingException("Couldn't import the container.", e);
		} catch (EntityNotFoundException e) {
			regionService.store(region);
		}

		if (container.getClubs() != null) {
			for (ClubContainer clubContainer : container.getClubs()) {
				if (clubContainer.getClub() != null) {
					clubContainer.getClub().setRegionId(region.getId());
				}
				clubImportService.importClubs(clubContainer);
			}
		}
		
		if (container.getCompetitions() != null) {
			for (CompetitionContainer competitionContainer : container.getCompetitions()) {
				if (competitionContainer.getCompetition() != null) {
					competitionContainer.getCompetition().setRegionId(region.getId());
				}
				competitionImportService.importCompetition(competitionContainer);
			}
		}
		LOGGER.info("END import of regions.");
	}

}
